package com.graduate.game.user.controller;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduate.game.user.mapper.UserMapper;
import com.graduate.game.user.pojo.User;
import com.graduate.game.user.pojo.userdata;
import com.graduate.game.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
@Controller
@ResponseBody
@RequestMapping("/user")
@CrossOrigin(origins = "*", maxAge = 3600)
public class userController {
      @Autowired
     private UserService userService;
      //登陆验证，传入username ,返回password
    @RequestMapping("/login")
    public String check(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        System.out.println(s1.length());
        System.out.println(userService.checkPassword(s1));
        return userService.checkPassword(s1);
    }

    @RequestMapping("/json")
    public String testJason(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        // ObjectMapper是jackson对象映射器，一个类，将对象转换成json字符串
        ObjectMapper objectmapper = new ObjectMapper();
        List<User> users = userService.lookAll();
        //将java对象转换为json字符串
        String str = objectmapper.writeValueAsString(users);
        return str;
    }
    //获取user表得全部username
    @RequestMapping("/username")
    public String lookUsername(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        //获取全部用户username，注册判断是否已注册过
        response.setHeader("Access-Control-Allow-Origin","*");
        ObjectMapper objectmapper = new ObjectMapper();
        List<String> usernames = userService.lookUsername();
        String str = objectmapper.writeValueAsString(usernames);
        return str;
    }
    //注册用户
    @RequestMapping("/zhuce")
    public String addUSer(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        Map maps =(Map)JSON.parse(s1);
        User user = new User();
        userdata user_data = new userdata();
        //获取系统时间
        Date date = new Date(System.currentTimeMillis()-5400000);
        //添加user表
        user.setName((String) maps.get("name"));
        user.setUsername((String) maps.get("username"));
        user.setPhone((String) maps.get("iphone"));
        user.setEmail((String) maps.get("email"));
        user.setPassword((String) maps.get("password"));
        user.setUpdated_time(date);
        user.setUpdated_by(1);
        //1代表普通用户，0代表管理员用户
        user.setVersion(1);
        //1代表逻辑存在，0代表逻辑删除
        user.setDeleted(1);
        //添加user_data表
        user_data.setUsername((String) maps.get("username"));
        user_data.setGold(0);
        user_data.setExperience(0);
        user_data.setUpdatedTime(date);
        user_data.setUpdatedBy(1);
        //执行service
        userService.zhuCe(user);
        userService.zhuCe_data(user_data);
        return s1;
    }
    @RequestMapping("/version")
    public String getversion(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        String version = userService.getVersion(s1);
        System.out.println(version);
        return version;
    }
    //SELECT * FROM  USER  ORDER BY RAND() LIMIT 1;
    @RequestMapping("/OneUser")
    public User getOneUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        return userService.getOneUser(s1);
    }
    @RequestMapping("/updateuser")
    public String updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map)JSON.parse(s1);
        System.out.println("更新请求");
        User user = new User();
        user.setUsername((String) maps.get("username"));
        user.setName((String) maps.get("name"));
        user.setEmail((String) maps.get("email"));
        user.setPhone((String) maps.get("iphone"));
        user.setSign((String) maps.get("sign"));
        userService.updateUser(user);
        return "更新成功";
    }
    //模糊查询用户
    @RequestMapping("/serchUser")
    public List<User> serchUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        System.out.println("进入用户模糊查询");
        String s1 = request.getReader().readLine();
         return userService.serchUsers(s1);
    }
}
